<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/07/10
 * Time: 18:05
 */

class AhpRecommendedController extends BaseController{

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-10 18:06:29
     * @Description: 当季推荐
     * @link: http://www.activity.com/ahp/ahp3.0/recommended_season?token=3de967e60c9bcd4b85a5858bac630b1c&target_url_one=www.baidu.com
     */
    public function recommended_season(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'type' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }

            if(Input::get('type') != 1){
                $season = RecommendedSeason::first();
                if(is_null($season)){
                    $season = new RecommendedSeason();
                    $season->created_at = date("Y-m-d H:i:s",time());
                    $season->created_by = $mkt_user->id;
                }
                $season->target_url_one = Input::get("target_url_one");
                $season->target_url_two = Input::get("target_url_two");
                $season->target_url_three = Input::get("target_url_three");
                $season->image_one = Input::get("image_one");
                $season->image_two = Input::get("image_two");
                $season->image_three = Input::get("image_three");
                $season->is_open = Input::get("is_open");
                $season->updated_at = date("Y-m-d H:i:s",time());
                $season->updated_by = $mkt_user->id;
                $season->save();
            }
            $recommended = RecommendedSeason::first();
            if(!is_null($recommended)){
                $recommended->url = Config::get('config.ahp_upload_files_url');
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $recommended);

        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "recommended_season", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}